import { SandTable } from './../pages/home/house/lou/control/SandTable';
// import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { getLouList, getSand, getRent, getSecond } from '@/api/getToutist';
import { getBuildRent, getBuildSale } from '@/api/getShop';
export interface IndexModelType {
  namespace: 'house';
  state: any;
  effects: {
    init: any;
    add: any;
    int: any;
    rent: any;
    second: any;
    buildrent: any;
  };
  reducers: {
    GET_LIST: any;
    DEL: any;
    ADD: any;
    SEARCH: any;
    SAND: any;
    EDIT: any;
    RENT: any;
    SECOND: any;
    SET: any;
    ADDRENT: any;
    rendSeach: any;
    EDITRENT: any;
    DELRENT: any;
    secondSearch: any;
    ADDSECOND: any;
    EDITSECOND: any;
    DELSECOND: any;
    ADDSAND: any;
    DELSAND: any;
    EDITSAND: any;
    BUILDRENT: any;
    BUILDRENTSEARCH: any;
    BUILDRENTDEL: any;
    BUILDRENTADD: any;
    BUILDRENTEDIT: any;
    BUILDSETRENT: any;
  };
  subscriptions: any;
}

const IndexModel: IndexModelType = {
  namespace: 'house',
  state: {
    list: [],
    editList: {},
    sandList: [],
    rentList: [],
    secondList: [],
    buildrentEdit: {},
    buildrentlist: [],
    buildsalelist: [],
  },

  effects: {
    *init(action: any, { call, put }) {
      // const {} = action;
      const data = yield call(getLouList);
      yield put({ type: 'GET_LIST', list: data.data.data });
    },
    *add(action: any, { call, put }) {
      console.log(action);
      const { title, address, price, update_time, status, ordid } =
        action.values;
      const data = yield call(getLouList);
      const arr = JSON.parse(JSON.stringify(data.data.data));
      arr.push({
        title,
        address,
        price,
        update_time: new Date().getTime(),
        status,
        ordid,
        id: arr.length + 3,
      });
      yield put({ type: 'ADD', data: arr });
    },
    *int(action: any, { call, put }) {
      const arr = yield call(getSand);
      console.log(arr);
      yield put({ type: 'SAND', data: arr.data.data });
    },
    *second(action: any, { call, put }) {
      const arr = yield call(getSecond);
      yield put({ type: 'SECOND', data: arr.data.data });
    },
    *rent(action: any, { call, put }) {
      const arr = yield call(getRent);
      yield put({ type: 'RENT', data: arr.data.data });
    },
    *buildrent(action: any, { call, put }) {
      const arr = yield call(getBuildRent);
      yield put({ type: 'BUILDRENT', data: arr.data.data });
    },
    *buildsale(action: any, { call, put }) {
      const arr = yield call(getBuildSale);
      yield put({ type: 'BUILDSALE', data: arr.data.data });
    },
  },
  reducers: {
    // 列表
    GET_LIST(state: any, action: any) {
      state.list = action.list;
      return {
        ...state,
      };
    },
    // build
    BUILDRENT(state: any, action: any) {
      state.buildrentlist = action.data;
      return {
        ...state,
      };
    },
    // buildrent 搜索
    BUILDRENTSEARCH(state: any, action: any) {
      const { value } = action;
      const arr = JSON.parse(JSON.stringify(state.buildrentlist));
      const newarr: any = [];
      arr.filter((item: any) => {
        if (item.title.includes(value)) {
          return newarr.push(item);
        }
      });
      state.buildrentlist = newarr;
      return {
        ...state,
      };
    },
    // buildrent 删除
    BUILDRENTDEL(state: any, action: any) {
      console.log(action);
      const arr = JSON.parse(JSON.stringify(state.buildrentlist));
      arr.forEach((item: any, index: number) => {
        if (item.id === action.id) {
          arr.splice(index, 1);
        }
      });
      state.buildrentlist = arr;
      return {
        ...state,
      };
    },

    // buildrent 新建
    BUILDRENTADD(state: any, action: any) {
      console.log(action);

      const arr = JSON.parse(JSON.stringify(state.buildrentlist));
      arr.push({
        id: state.buildrentlist.length + 1,
        estate_name: action.values.estate_name,
        title: action.values.title,
        price: action.values.price,
        acreage: action.values.acreage,
        status: 1,
        ordid: 1000,
      });
      state.buildrentlist = arr;
      return {
        ...state,
      };
    },

    // buildrentedit 编辑
    BUILDRENTEDIT(state: any, action: any) {
      const { id } = action;
      const arr = JSON.parse(JSON.stringify(state.buildrentlist));
      const newarr = arr.find((item: any) => item.id == id);
      // console.log(newarr);
      state.buildrentEdit = newarr;
      return {
        ...state,
      };
    },
    // set编辑
    BUILDSETRENT(state: any, action: any) {
      console.log(action);
      const arr = JSON.parse(JSON.stringify(state.buildrentlist));
      // console.log(arr);

      arr.forEach((item: any, index: number) => {
        if (item.id === action.values.id) {
          arr[index] = action.values;
        }
      });
      state.buildrentlist = arr;
      return {
        ...state,
      };
    },
    //删除
    DEL(state: any, action: any) {
      console.log(action);
      const arr = JSON.parse(JSON.stringify(state.list));
      arr.forEach((item: any, index: number) => {
        if (item.id === action.id) {
          arr.splice(index, 1);
        }
      });
      state.list = arr;
      return {
        ...state,
      };
    },
    // 添加
    ADD(state: any, action: any) {
      state.list = action.data;
      return {
        ...state,
      };
    },
    // 搜索
    SEARCH(state: any, action: any) {
      console.log(action);
      const { value } = action;
      const arr = JSON.parse(JSON.stringify(state.list));
      const newarr: any = [];
      arr.filter((item: any) => {
        if (item.title.includes(value)) {
          return newarr.push(item);
        }
      });
      state.list = newarr;
      return {
        ...state,
      };
    },
    // // 编辑
    EDIT(state: any, action: any) {
      const { id } = action;
      const arr = JSON.parse(JSON.stringify(state.list));
      const newarr = arr.find((item: any) => item.id == id);
      // console.log(newarr);
      state.editList = newarr;
      return {
        ...state,
      };
    },

    // 沙盘渲染
    SAND(state: any, action: any) {
      state.sandList = action.data;
      return {
        ...state,
      };
    },

    SECOND(state: any, action: any) {
      state.secondList = action.data;
      return {
        ...state,
      };
    },

    RENT(state: any, action: any) {
      state.rentList = action.data;
      return {
        ...state,
      };
    },

    // //////////////////
    SET(state: any, action: any) {
      console.log(action);
      const arr = JSON.parse(JSON.stringify(state.list));
      console.log(arr);

      arr.forEach((item: any, index: number) => {
        if (item.id === action.values.id) {
          arr[index] = action.values;
        }
      });
      state.list = arr;
      return {
        ...state,
      };
    },

    // 租房搜索
    rendSeach(state: any, action: any) {
      const arr = JSON.parse(JSON.stringify(state.rentList));
      const { value } = action;
      const newarr: any = [];
      arr.filter((item: any) => {
        if (item.title.includes(value)) {
          return newarr.push(item);
        }
      });

      state.rentList = newarr;
      return {
        ...state,
      };
    },

    // 租房新建
    ADDRENT(state: any, action: any) {
      console.log(action);
      const arr = JSON.parse(JSON.stringify(state.rentList));
      arr.push({
        id: arr.length + 1,
        estate_name: action.values.username,
        title: '西岸首府两房两厅120平方',
        acreage: 120,
        ordid: 1000,
        status: 2,
      });
      state.rentList = arr;
      return {
        ...state,
      };
    },

    // 编辑租房
    EDITRENT(state: any, action: any) {
      console.log(action);
      const arr = JSON.parse(JSON.stringify(state.rentList));
      arr.forEach((item: any, index: number) => {
        if (item.id == action.id) {
          arr[index].estate_name = action.values.username;
        }
      });
      state.rentList = arr;
      return {
        ...state,
      };
    },

    // 删除租房
    DELRENT(state: any, action: any) {
      const arr = JSON.parse(JSON.stringify(state.rentList));
      const index = arr.findIndex((item: any) => item.id == action.item.id);
      arr.splice(index, 1);
      state.rentList = arr;
      return {
        ...state,
      };
    },

    // 搜索二手房
    secondSearch(state: any, action: any) {
      const arr = JSON.parse(JSON.stringify(state.secondList));
      const { value } = action;
      const newarr: any = [];
      arr.filter((item: any) => {
        if (item.title.includes(value)) {
          return newarr.push(item);
        }
      });

      state.secondList = newarr;
      return {
        ...state,
      };
    },
    // 新建
    ADDSECOND(state: any, action: any) {
      console.log(action);
      const arr = JSON.parse(JSON.stringify(state.secondList));
      arr.push({
        id: arr.length + 1,
        estate_name: action.values.username,
        title: '西岸首府两房两厅120平方',
        acreage: 120,
        ordid: 1000,
        status: 2,
      });
      state.secondList = arr;
      return {
        ...state,
      };
    },

    EDITSECOND(state: any, action: any) {
      console.log(action);
      const arr = JSON.parse(JSON.stringify(state.secondList));
      arr.forEach((item: any, index: number) => {
        if (item.id == action.id) {
          arr[index].estate_name = action.values.username;
        }
      });
      state.secondList = arr;
      return {
        ...state,
      };
    },

    // 删除二手房
    DELSECOND(state: any, action: any) {
      const arr = JSON.parse(JSON.stringify(state.secondList));
      const index = arr.findIndex((item: any) => item.id == action.item.id);
      arr.splice(index, 1);
      state.secondList = arr;
      return {
        ...state,
      };
    },

    // 添加沙盘
    ADDSAND(state: any, action: any) {
      console.log(action);
      const { title, floor, sale_status, status } = action.values;

      const arr = JSON.parse(JSON.stringify(state.sandList));
      arr.push({
        id: arr.length + 1,
        title: title,
        house_id: floor,
        sale_status: sale_status,
        status: status,
      });
      state.sandList = arr;
      return {
        ...state,
      };
    },

    // 删除沙盘
    DELSAND(state: any, action: any) {
      const arr = JSON.parse(JSON.stringify(state.sandList));
      const index = arr.findIndex((item: any) => item.id == action.item.id);
      arr.splice(index, 1);
      state.sandList = arr;
      return {
        ...state,
      };
    },

    // 编辑沙盘
    EDITSAND(state: any, action: any) {
      console.log(action);

      const arr = JSON.parse(JSON.stringify(state.sandList));
      arr.forEach((item: any, index: any) => {
        if (item.id == action.id) {
          arr[index].title = action.values.title;
        }
      });
      state.sandList = arr;
      return {
        ...state,
      };
    },
  },
  subscriptions: {
    setinit({ dispatch, history }: any) {
      dispatch({
        type: 'init',
      });
    },
    buildrent({ dispatch, history }: any) {
      dispatch({
        type: 'buildrent',
      });
    },
  },
};

export default IndexModel;
